home *** CD-ROM | disk | FTP | other *** search
- /****i* SOURCE_FILE/INFO
- *
- * NAME
- * ResourceBundle.js
- *
- * USAGE
- * Part of Netobjects JavaScript Library.
- *
- * COPYRIGHT
- * Copyright ⌐ 2000-2005 Website Pros, Inc.
- * All Rights Reserved.
- *
- * This is an unpublished work protected by Website Pros, Inc.
- * as a trade secret, and is not to be used or disclosed except as
- * expressly provided in a written license agreement executed by
- * you and Website Pros, Inc.
- *
- * <copyright@websitepros.com>
- *
- * NOTES
- * JavaScript code.
- *
- *****/
-
- if (!IS.isModuleInitialized("IS.NOF.UTIL.ResourceBundle"))
- {
- /****h* NOF_JavaScript_Library/NOF.UTIL.ResourceBundle
- *
- * NAME
- * NOF.UTIL.ResourceBundle
- *
- * DESCRIPTION
- * Resource bundles contain locale-specific objects.
- * When your program needs a locale-specific resource,
- * a String for example, your program can load it from
- * the resource bundle that is appropriate for the current
- * user's locale. In this way, you can write program code
- * that is largely independent of the user's locale isolating
- * most, if not all, of the locale-specific information in resource bundles.
- *
- * External dependencies: NOF.App, NOF.UTIL.Locale, NOF.IO.File, NOF.UTIL.PropertyResourceBundle
- ****/
-
- /**
- * Constructor
- */
- function UTIL_ResourceBundle(){
- this.__proto__ = UTIL_ResourceBundle.prototype;
- }
- {
- var nof_unicode_re = /\\u/;
- function NOF_UnicodeEval(val) {
- if (nof_unicode_re.test(val)) {
-
- var expVal = "";
-
- try {
- val = val.replace(/\"/g, '\\"');
- val = val.replace(/\'/g, "\\'");
- eval('expVal = "' + val + '";');
- } catch(e) {
- return val;
- }
- return expVal;
- } else {
- return val;
- }
- }
-
- var method = UTIL_ResourceBundle.prototype;
-
- /**
- * Return a NOF.UTIL.PropertyResourceBundle based on the given resource content <br>
- * @param content - the resource content
- * @param toRetObj - the property resource bundle to be instantiated
- * (if not specified a new object will be created)
- *
- **/
- method.getProperties = function ( /*string*/ content, /*[ NOF.UTIL.PropertyResourceBundle*/ toRetObj /*]*/, /*NOF.UTIL.Locale*/ locale) {
- if (arguments.length < 2)
- toRetObj = new NOF.UTIL.PropertyResourceBundle( "anon" );
- // escape native2ascii begin of file marker
- if (content.indexOf("\\ufeff") == 0) {
- content = content.substring("\\ufeff".length);
- }
- var hashList = content.split('\n');
-
- var separatorIndex, key, val;
- var line;
- for (var i=0; i<hashList.length; i++) {
- line = hashList[i];
- if (line.length==0 || line.indexOf("#")==0) {
- continue;
- }
-
- separatorIndex = line.indexOf('=');
- if (separatorIndex>-1) {
- key = line.substring(0, separatorIndex);
- //key = key.replace (/\s/g, "");
- key = key.trim();
- val = line.substring( separatorIndex+1, line.length);
-
- if (val.lastIndexOf("\\") == val.length-1) {
- val = NOF_UnicodeEval( val.substring(0,val.length-1)) + "\n";
-
- while ( (i + 1 < hashList.length) ) {
- i++;
- line = hashList[i];
- if (line.length==0) break;
- if (line.charAt(line.length - 1) == '\\') {
- val += NOF_UnicodeEval(line.substring(0, line.length-1)) + "\n";
- } else {
- val += NOF_UnicodeEval(line);
- break;
- }
- }
- } else {
- val = NOF_UnicodeEval(val);
- }
-
- //pgLogger.info(key + "=" + val);
-
- /*
- while (val.lastIndexOf("\\") == val.length-1) {
- i++;
- val = val.substring(0, val.length-1) + "\n" + hashList[i];
- }*/
- if (key == "@import") {
-
- //search for keys, separated by comma, enclosed in brakets { }
- var restrictToKeys = null;
- if (val.charAt(val.length-1) == "}") {
- var bIndex = val.indexOf("{");
- restrictToKeys = val.substring(bIndex + 1, val.length-1).split(",");
- val = val.substring(0, bIndex);
- }
- var importedProp = this.getBundle(val, locale);
- if (importedProp == null) {
- continue;
- }
- var importedKeys = (restrictToKeys) ? restrictToKeys : importedProp.getKeys();
- for (var j=0; j < importedKeys.length; j++) {
- //alert(toRetObj.getID() + " - key:" + importedKeys[j] + "=\n" + importedProp.getProperty(importedKeys[j]) );
- toRetObj.setProperty(importedKeys[j], importedProp.getProperty(importedKeys[j]));
- }
- } else {
- // use key=$property=anAlreadyDefinedKey
- if (val.indexOf("$property=") == 0) {
- var sameValueKey = val.substring(10); //"$property=".length == 10
- val = toRetObj.getProperty(sameValueKey);
- }
- //
- toRetObj.setProperty(key,val);
- }
- }
- }
-
- return toRetObj;
- }
-
- /**
- * Return a NOF.UTIL.PropertyResourceBundle based on the given resource file <br>
- * @param resource - the path and name (without extension) of the resource file
- * @param locale - the locale
- **/
- method.getBundle = function (/*string*/ resource, /*NOF.UTIL.Locale*/ locale) {
- var usingDefault = false;
- var defaultLocale = (new NOF.UTIL.Locale()).getDefault();
-
- if (defaultLocale.equals(locale) || locale == null){
- locale = defaultLocale;
- usingDefault = true;
- }
-
- var resourceName = "";
- var resourceToFind = "";
-
- var libPathsArray = NOF.App.getLibsPath(); //NOF.System.getLibsPath();
- for (var i=0; i<libPathsArray.length; i++) {
- resourceName = libPathsArray[i] + "\\"+resource;
- var file = null;
-
- try{
- //try to locate the resource in the requested locale
- file = this.getResourceFile( resourceName + "_" + locale.toString() );
-
- if (file == null)
- file = this.getResourceFile( resourceName + "_" + locale.getLanguage() );
-
- //try to locate the resource in the default locale
- if ( file == null && !usingDefault ){
- file = this.getResourceFile( resourceName + "_" + defaultLocale.toString() );
-
- if (file == null)
- file = this.getResourceFile( resourceName + "_" + defaultLocale.getLanguage() );
- }
-
- //finaly try to locate the resource as is
- if (file == null)
- file = this.getResourceFile( resourceName );
-
- if (file != null) // a match was found
- return this.createResource( file, locale );
-
- }catch(e){}
- }
-
- return null; //no match found at all. give up
- }
-
- // private methods
- /**
- * Create PropertyResourceBundle from file name
- **/
- method.createResource = function ( /*NOF.IO.File*/ file, /*NOF.UTIL.Locale*/ locale) {
- var resObjName = file.pathName;
- var content = file.readContent();
- re = /\.js$/;
- if( re.test( resObjName) ){
- try{
- eval(content);
- var z;
- eval("z = new "+resObjName+"()");
- return z;
- }catch( notPropertyResourceBundle ){
- return null;
- }
- }else{
- if( content != null ){
- //it contains property.key and property.value under properties node
- //alert("resObjName = " + resObjName);
- var toRetObj = new NOF.UTIL.PropertyResourceBundle( resObjName );
- this.getProperties(content, toRetObj, locale);
- return toRetObj;
- }
- }
- return null;
- }
-
- /**
- * Create and return NOF.IO.File object based on file name.
- **/
- method.getResourceFile = function ( /*string*/ root ){
- var file = new NOF.IO.File( root + ".js" );
- if ( file.exists() )
- return file;
-
- file = new NOF.IO.File( root + ".properties" );
- if ( file.exists() )
- return file;
-
- return null;
- }
- }
-
- UTIL.__proto__.ResourceBundle = new UTIL_ResourceBundle();
- }
-